//
//  CXOAuthVIewController.swift
//  Sweibo
//
//  Created by 陈旭 on 16/6/25.
//  Copyright © 2016年 陈旭. All rights reserved.
//

import UIKit

class CXOAuthVIewController: UIViewController {

    override func loadView() {
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //MARK: - 监听事件
    @objc private func cancelClick(){
    
        dismissViewControllerAnimated(true, completion: nil)
    }
    @objc private func autoFillClick(){
    
        print("按钮被点击了")
    }
    //MARK: - 设置导航
    private func setupNav(){
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelClick")
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoFillClick")
        
    navigationItem.title = "OAuth登录"
    }
    
    //MARK: - 设置视图
    private func setupUI(){
    
        view.backgroundColor = UIColor.whiteColor()
        setupNav()
    }
    
    //MARK: - 懒加载控件
    private lazy var webView:UIWebView = {
    
        let view = UIWebView()
        view.delegate = self
        let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(APPKEY)&redirect_uri=\(REDIRECT_URI)")!
        let request = NSURLRequest(URL: url)
        view.loadRequest(request)
        return view
    }()
}
extension CXOAuthVIewController: UIWebViewDelegate {
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        print(request.URL?.absoluteString)
        return true
    }
}


























